package common.util.mail;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ncp.model.biz.registerlogin.RegisterBiz;
import common.util.security.EncryptUtil;

@SuppressWarnings("serial")
public class registerActiveServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String msg = "";
		String ActiveCode = request.getParameter("ActiveCode");
		EncryptUtil encrypt = new EncryptUtil();
		String ActiveCodeSHA=encrypt.sha(ActiveCode);
	//	System.out.println(ActiveCodeSHA);
		RegisterBiz registerbiz = new RegisterBiz();
		if(ActiveCode==null){
			msg = "激活码无效！请重新注册！";
			out.print("<script language=javascript>alert('"+msg+"');window.parent.location.href='pages/registerlogin/register.jsp'</script>"); 
		}else if(registerbiz.isActived(ActiveCodeSHA)){
			msg = "该用户已经激活！";
			out.print("<script language=javascript>alert('"+msg+"');window.parent.location.href='index.jsp'</script>");
		}else if(!registerbiz.isActived(ActiveCodeSHA)){
			// 设置激活成功的标志
			if(registerbiz.UserActive(ActiveCodeSHA)){
				msg = "激活成功！";
				out.print("<script language=javascript>alert('"+msg+"');window.parent.location.href='index.jsp'</script>");
			}else{
				msg = "激活失败，请重新注册！";
				out.print("<script language=javascript>alert('"+msg+"');window.parent.location.href='pages/registerlogin/register.jsp'</script>");
			}
		}
	}
}
